home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MetalScrollButton.java < prev    next >
Text File  |  1998-06-30  |  9KB  |  324 lines

  1. /*
  2.  * @(#)MetalScrollButton.java    1.11 98/03/10
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.metal;
  22.  
  23. import java.awt.Dimension;
  24. import java.awt.Graphics;
  25. import java.awt.Color;
  26. import java.awt.Polygon;
  27.  
  28. import com.sun.java.swing.*;
  29.  
  30. import com.sun.java.swing.plaf.basic.BasicArrowButton;
  31.  
  32.  
  33. /**
  34.  * JButton object for Metal scrollbar arrows.
  35.  * <p>
  36.  * Warning: serialized objects of this class will not be compatible with
  37.  * future swing releases.  The current serialization support is appropriate
  38.  * for short term storage or RMI between Swing1.0 applications.  It will
  39.  * not be possible to load serialized Swing1.0 objects with future releases
  40.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  41.  * baseline for the serialized form of Swing objects.
  42.  *
  43.  * @version 1.11 03/10/98
  44.  * @author Jeff Shapiro
  45.  */
  46. public class MetalScrollButton extends BasicArrowButton
  47. {
  48.   private static Color shadowColor;
  49.   private static Color highlightColor;
  50.   private boolean isFreeStanding = false;
  51.  
  52.   private int buttonWidth;
  53.  
  54.         public MetalScrollButton( int direction, int width, boolean freeStanding )
  55.         {
  56.             super( direction );
  57.  
  58.         shadowColor = UIManager.getColor("ScrollBar.shadow");
  59.         highlightColor = UIManager.getColor("ScrollBar.highlight");
  60.  
  61.             buttonWidth = width;
  62.         isFreeStanding = freeStanding;
  63.         }
  64.  
  65.         public void setFreeStanding( boolean freeStanding )
  66.         {
  67.         isFreeStanding = freeStanding;
  68.         }
  69.  
  70.     public void paint( Graphics g )
  71.         {
  72.         boolean isEnabled = getParent().isEnabled();
  73.  
  74.         Color arrowColor = isEnabled ? MetalLookAndFeel.getControlInfo() : MetalLookAndFeel.getControlDisabled();
  75.         boolean isPressed = getModel().isPressed();
  76.         int width = getSize().width;
  77.         int height = getSize().height;
  78.         int w = width;
  79.         int h = height;
  80.         int arrowHeight = (height+1) / 4;
  81.         int arrowWidth = (height+1) / 2;
  82.  
  83.         if ( isPressed )
  84.         {
  85.             g.setColor( MetalLookAndFeel.getControlShadow() );
  86.         }
  87.         else
  88.             {
  89.             g.setColor( getBackground() );
  90.         }
  91.  
  92.         g.fillRect( 0, 0, width, height );
  93.  
  94.         if ( getDirection() == NORTH )
  95.         {
  96.             if ( !isFreeStanding ) {
  97.             width += 2;
  98.         }
  99.  
  100.             // Draw the arrow
  101.             g.setColor( arrowColor );
  102.         int startY = ((h+1) - arrowHeight) / 2;
  103.         int startX = (w / 2);
  104.         //            System.out.println( "startX :" + startX + " startY :"+startY);
  105.         for (int line = 0; line < arrowHeight; line++) {
  106.             g.drawLine( startX-line, startY+line, startX +line+1, startY+line);
  107.         }
  108.     /*    g.drawLine( 7, 6, 8, 6 );
  109.         g.drawLine( 6, 7, 9, 7 );
  110.         g.drawLine( 5, 8, 10, 8 );
  111.         g.drawLine( 4, 9, 11, 9 );*/
  112.             
  113.         if (isEnabled) {
  114.             g.setColor( highlightColor );
  115.  
  116.             if ( !isPressed )
  117.             {
  118.             g.drawLine( 1, 1, width - 3, 1 );
  119.             g.drawLine( 1, 1, 1, height - 1 );
  120.             }
  121.  
  122.             g.drawLine( width - 1, 1, width - 1, height - 1 );
  123.  
  124.             g.setColor( shadowColor );
  125.             g.drawLine( 0, 0, width - 2, 0 );
  126.             g.drawLine( 0, 0, 0, height - 1 );
  127.             g.drawLine( width - 2, 2, width - 2, height - 1 );
  128.         } else {
  129.             MetalUtils.drawDisabledBorder(g, 0, 0, width, height+1);
  130.         }
  131.             if ( !isFreeStanding ) {
  132.             width -= 2;
  133.         }
  134.         }
  135.         else if ( getDirection() == SOUTH )
  136.         {
  137.             if ( !isFreeStanding ) {
  138.             width += 2;
  139.             height += 1;
  140.         }
  141.  
  142.             // Draw the arrow
  143.             g.setColor( arrowColor );
  144.  
  145.         int startY = (((h+1) - arrowHeight) / 2)+ arrowHeight-1;
  146.         int startX = (w / 2);
  147.  
  148.         //        System.out.println( "startX2 :" + startX + " startY2 :"+startY);
  149.  
  150.         for (int line = 0; line < arrowHeight; line++) {
  151.             g.drawLine( startX-line, startY-line, startX +line+1, startY-line);
  152.         }
  153.  
  154.     /*    g.drawLine( 4, 5, 11, 5 );
  155.         g.drawLine( 5, 6, 10, 6 );
  156.         g.drawLine( 6, 7, 9, 7 );
  157.         g.drawLine( 7, 8, 8, 8 ); */
  158.  
  159.         if (isEnabled) {
  160.             g.setColor( highlightColor );
  161.  
  162.             if ( !isPressed )
  163.             {
  164.             g.drawLine( 1, 0, width - 3, 0 );
  165.             g.drawLine( 1, 0, 1, height - 3 );
  166.             }
  167.  
  168.             g.drawLine( 1, height - 1, width - 1, height - 1 );
  169.             g.drawLine( width - 1, 0, width - 1, height - 1 );
  170.  
  171.             g.setColor( shadowColor );
  172.             g.drawLine( 0, 0, 0, height - 2 );
  173.             g.drawLine( width - 2, 0, width - 2, height - 2 );
  174.             g.drawLine( 2, height - 2, width - 2, height - 2 );
  175.         } else {
  176.             MetalUtils.drawDisabledBorder(g, 0,-1, width, height+1);
  177.         }
  178.  
  179.             if ( !isFreeStanding ) {
  180.             width -= 2;
  181.             height -= 1;
  182.         }
  183.         }
  184.         else if ( getDirection() == EAST )
  185.         {
  186.             if ( !isFreeStanding ) {
  187.             height += 2;
  188.             width += 1;
  189.         }
  190.  
  191.             // Draw the arrow
  192.             g.setColor( arrowColor );
  193.  
  194.         int startX = (((w+1) - arrowHeight) / 2) + arrowHeight-1;
  195.         int startY = (h / 2);
  196.  
  197.         //System.out.println( "startX2 :" + startX + " startY2 :"+startY);
  198.  
  199.         for (int line = 0; line < arrowHeight; line++) {
  200.             g.drawLine( startX-line, startY-line, startX -line, startY+line+1);
  201.         }
  202.  
  203.  
  204. /*        g.drawLine( 5, 4, 5, 11 );
  205.         g.drawLine( 6, 5, 6, 10 );
  206.         g.drawLine( 7, 6, 7, 9 );
  207.         g.drawLine( 8, 7, 8, 8 );*/
  208.  
  209.         if (isEnabled) {
  210.             g.setColor( highlightColor );
  211.  
  212.             if ( !isPressed )
  213.             {
  214.                 g.drawLine( 0, 1, width - 3, 1 );
  215.                 g.drawLine( 0, 1, 0, height - 3 );
  216.             }
  217.  
  218.             g.drawLine( width - 1, 1, width - 1, height - 1 );
  219.             g.drawLine( 0, height - 1, width - 1, height - 1 );
  220.  
  221.             g.setColor( shadowColor );
  222.             g.drawLine( 0, 0,width - 2, 0 );
  223.             g.drawLine( width - 2, 2, width - 2, height - 2 );
  224.             g.drawLine( 0, height - 2, width - 2, height - 2 );
  225.         } else {
  226.             MetalUtils.drawDisabledBorder(g,-1,0, width+1, height);
  227.         }
  228.             if ( !isFreeStanding ) {
  229.             height -= 2;
  230.             width -= 1;
  231.         }
  232.         }
  233.         else if ( getDirection() == WEST )
  234.         {
  235.             if ( !isFreeStanding ) {
  236.             height += 2;
  237.             width += 1;
  238.             g.translate( -1, 0 );
  239.         }
  240.  
  241.             // Draw the arrow
  242.             g.setColor( arrowColor );
  243.  
  244.         int startX = (((w+1) - arrowHeight) / 2);
  245.         int startY = (h / 2);
  246.  
  247.  
  248.         for (int line = 0; line < arrowHeight; line++) {
  249.             g.drawLine( startX+line, startY-line, startX +line, startY+line+1);
  250.         }
  251.  
  252.     /*    g.drawLine( 6, 7, 6, 8 );
  253.         g.drawLine( 7, 6, 7, 9 );
  254.         g.drawLine( 8, 5, 8, 10 );
  255.         g.drawLine( 9, 4, 9, 11 );*/
  256.  
  257.         if (isEnabled) {
  258.             g.setColor( highlightColor );
  259.  
  260.  
  261.             if ( !isPressed )
  262.             {
  263.                 g.drawLine( 1, 1, width - 1, 1 );
  264.             g.drawLine( 1, 1, 1, height - 3 );
  265.             }
  266.  
  267.             g.drawLine( 1, height - 1, width - 1, height - 1 );
  268.         
  269.             g.setColor( shadowColor );
  270.             g.drawLine( 0, 0, width - 1, 0 );
  271.             g.drawLine( 0, 0, 0, height - 2 );
  272.             g.drawLine( 2, height - 2, width - 1, height - 2 );
  273.         } else {
  274.             MetalUtils.drawDisabledBorder(g,0,0, width+1, height);
  275.         }
  276.  
  277.             if ( !isFreeStanding ) {
  278.             height -= 2;
  279.             width -= 1;
  280.             g.translate( 1, 0 );
  281.         }
  282.         }
  283.         }
  284.  
  285.         public Dimension getPreferredSize()
  286.         {
  287.         if ( getDirection() == NORTH )
  288.         {
  289.             return new Dimension( buttonWidth, buttonWidth - 2 );
  290.         }
  291.         else if ( getDirection() == SOUTH )
  292.             {
  293.             return new Dimension( buttonWidth, buttonWidth - (isFreeStanding ? 1 : 2) );
  294.         }
  295.         else if ( getDirection() == EAST )
  296.             {
  297.             return new Dimension( buttonWidth - (isFreeStanding ? 1 : 2), buttonWidth );
  298.         }
  299.         else if ( getDirection() == WEST )
  300.             {
  301.             return new Dimension( buttonWidth - 2, buttonWidth );
  302.         }
  303.         else
  304.             {
  305.             return new Dimension( 0, 0 );
  306.         }
  307.         }
  308.  
  309.         public Dimension getMinimumSize()
  310.         {
  311.             return getPreferredSize();
  312.         }
  313.  
  314.         public Dimension getMaximumSize()
  315.         {
  316.             return new Dimension( Integer.MAX_VALUE, Integer.MAX_VALUE );
  317.         }
  318.     
  319.         public int getButtonWidth() {
  320.         return buttonWidth;
  321.     }
  322. }
  323.  
  324.